package com.concurrence.interrupt;
/**
 * erfd..
 *
 * @author Lizhong
 * @date 2019/4/4
 */
public class InterruptDemo {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        WriterReader writerReader = new WriterReader();
        Thread writer = new Thread(new Runnable() {
            @Override
            public void run() {
                writerReader.write();
            }
        });
        writer.start();

        Thread reader = new Thread(new Runnable() {
            @Override
            public void run() {
                writerReader.read();
            }
        });
        // 1.让写程序先运行
        Thread.sleep(100);
        reader.start();

        // 2.这里中断写线程可以被立即响应，writer正在运行
        writer.interrupt();
        // 3.尽管write中断但还没彻底，read没有获取到对象锁还处于阻塞状态
        reader.interrupt();
        System.out.println("write:" + writer.isInterrupted());  // 线程在运行 可中断
        // 4.但是处于阻塞态的线程不可以立即响应，可以看到在writer运行结束后reader的中断情况才改变
        Thread.sleep(3000);
        System.out.println("reader:" + reader.isInterrupted()); // fasle 中断无效
        // 5.再次发出中断请求
        writer.interrupt();
        System.out.println("reader:" + reader.isInterrupted()); // true 被中断
        Thread.sleep(3000);


    }
}
